<?php

namespace App\Admin\Controllers;

use App\Admin\Tool\Express;
use App\Models\Address;
use App\Models\Scorelist;
use App\Http\Controllers\Controller;
use App\Models\Wxuser;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Row;

class ScorelistController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('积分明细管理')
            ->description('description')
            ->body($this->grid());
    }
    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('详情')
            ->description('')
            ->row(function (Row $row)use($id) {
                $row->column(12, function (Column $column)use ($id) {
                    $column->append(Express::express($id));
                });
            });
    }


    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }
    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Scorelist);
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableEdit();
        });
        $grid->id('ID');
        $grid->title('奖品名称');
        $grid->column('微信图像')->display(function(){
            $result = Wxuser::find($this->wxuser_id)->avatarUrl;
            return $result;
        })->image(50,50);
        $grid->column('微信昵称')->display(function(){
            $result = Wxuser::find($this->wxuser_id);
            return json_decode($result)->nickname;
        });
        $grid->column('地址')->display(function(){
            if($this->address_id){
                $result = Address::find($this->address_id);
                return json_decode($result)->area.json_decode($result)->detail;
            }else{
                return '未填写地址';
            }
        });
        $grid->column('联系方式')->display(function(){
            if($this->address_id){
                $result = Address::find($this->address_id);
                return json_decode($result)->phone;
            }else{
                return '未填写联系方式';
            }
        });
        $grid->status('领取状态')->display(function($status){
            if ($status==0) {
                return "<span class='label bg-red'>未领取</span>";
            }elseif ($status==1) {
                return "<span class='label bg-green'>已领取</span>";
            }
        });
        $grid->ecompany('快递公司')->editable('select', [
            'SF' => '顺丰快递',
            'HTKY' => '百世快递',
            'ZTO' => '中通快递',
            'STO' => '申通快递',
            'YTO' => '圆通速递',
            'YD' => '韵达速递',
            'YZPY' => '邮政快递包裹',
            'EMS' => 'EMS',
            'HHTT' => '天天快递'
        ]);
        $grid->express_id('快递单号')->editable();

        return $grid;
    }
    protected function form()
    {
        $form = new Form(new Scorelist);
        $form->text('express_id');
        $form->text('ecompany');
        return $form;
    }
}
